Imports System
Imports System.EventArgs
Imports System.Windows.Forms.KeyEventArgs
Imports System.Windows.Forms.KeyPressEventArgs
Imports DevComponents.DotNetBar

Namespace xpComponent.Form
    Public NotInheritable Class KeyEventHandle
        Public Declare Function ToAscii Lib "user32.dll" (ByVal uVirtKey As Long, ByVal uScanCode As Long, ByRef lpbKeyState As Byte, ByRef lpwTransKey As Long, ByVal fuState As Long) As Long
        Public Declare Function GetKeyboardState Lib "user32.dll" (ByRef pbKeyState As Byte) As Long

        Public Shared Function GetCharFromKey(ByVal KeyCode As Integer) As String
            Dim KeyBoardState(255) As Byte
            Dim Out As Long
            If GetKeyboardState(KeyBoardState(0)) <> 0 Then
                If ToAscii(KeyCode, 0, KeyBoardState(0), Out, 0) <> 0 Then
                    If Out <= 255 Then
                        'GetCharFromKey = Chr$(Out)
                        GetCharFromKey = Microsoft.VisualBasic.ChrW(Out)
                    Else
                        'GetCharFromKey = Left$(StrConv(ChrW$(Out), vbUnicode), 1)
                        GetCharFromKey = Left$(StrConv(Microsoft.VisualBasic.ChrW(Out), VbStrConv.None), 1)
                    End If
                End If
            End If
        End Function

        Public Shared GotForeColor As System.Drawing.Color = System.Drawing.Color.Black
        Public Shared GotBackColor As System.Drawing.Color = System.Drawing.Color.SkyBlue

        Public Shared LostForeColor As System.Drawing.Color = System.Drawing.SystemColors.ControlText
        Public Shared LostBackColor As System.Drawing.Color = System.Drawing.SystemColors.Window

        Public Shared Sub TextBox_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim TextBox As System.Windows.Forms.TextBox = CType(sender, System.Windows.Forms.TextBox)
            TextBox.ForeColor = GotForeColor
            TextBox.BackColor = GotBackColor
        End Sub

        Public Shared Sub TextBox_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim TextBox As System.Windows.Forms.TextBox = CType(sender, System.Windows.Forms.TextBox)
            TextBox.ForeColor = LostForeColor
            TextBox.BackColor = LostBackColor
        End Sub
        Public Shared Sub Combobox_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim Combobox As System.Windows.Forms.ComboBox = CType(sender, System.Windows.Forms.ComboBox)
            Combobox.ForeColor = GotForeColor
            Combobox.BackColor = GotBackColor
        End Sub

        Public Shared Sub Combobox_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim Combobox As System.Windows.Forms.ComboBox = CType(sender, System.Windows.Forms.ComboBox)
            Combobox.ForeColor = LostForeColor
            Combobox.BackColor = LostBackColor
        End Sub

        Public Shared Sub NumOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim TextBoxs As System.Windows.Forms.TextBox = CType(sender, System.Windows.Forms.TextBox)
            If TextBoxs.Text.Length < 255 Then
                If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." Then
                    e.Handled = True
                Else
                    If (e.KeyChar < "0" Or e.KeyChar > "9") Then
                        e.Handled = True
                    End If
                End If
            Else
                e.Handled = True
            End If
        End Sub

        Public Shared Sub NumRealOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim TextBoxs As System.Windows.Forms.TextBox = CType(sender, System.Windows.Forms.TextBox)
            If TextBoxs.Text.Length < 255 Then
                If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." Then
                    e.Handled = True
                Else
                    If e.KeyChar = "." And TextBoxs.Text.IndexOf(".") <> -1 Then
                        e.Handled = True
                    End If
                End If
            Else
                e.Handled = True
            End If
        End Sub

        Public Shared Sub NullOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim TextBoxs As System.Windows.Forms.TextBox = CType(sender, System.Windows.Forms.TextBox)
            If TextBoxs.Text.Length < 255 Then
                If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." Then
                    e.Handled = True
                Else
                    If e.KeyChar = "." And TextBoxs.Text.IndexOf(".") <> -1 Then
                        e.Handled = True
                    Else
                        e.Handled = True
                    End If
                End If
                If e.KeyChar = Chr(System.Windows.Forms.Keys.Delete) Then
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        End Sub
    End Class
End Namespace